package com.fenyin.samples.study.parttern.struct.flyweight.example1;  

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
 *@Title: 复合享元模式
 *<p>MessageFactory：享元工厂角色</p>
 *@Author:fengzhichao  
 *@Since:2012-9-29  
 *@Version:1.1.0
 */
public class MessageFactory {
	
	/**Message缓存对象*/
	private static Map<Integer,Message> messageHash = new HashMap<Integer,Message>();
	
	/**
	 * 获取单纯享元对象
	 * @param id
	 * @param content
	 * @return  
	 * @Description:
	 */
	public static Message getMessage(int id,String content){
		Message message = null;
		message = messageHash.get(id);
		if(message == null){
			message = new PhoneMessage(id,content);
			messageHash.put(id, message);
		}
		return message;
	}
	
	/**
	 * 获取复合享元对象
	 * @param map
	 * @return  
	 * @Description:
	 */
	public static Message getMessage(Map<Integer,String> map){
		PhoneBatchMessage message = new PhoneBatchMessage();
		for(Entry<Integer,String> entry : map.entrySet()){
			Message single = getMessage(entry.getKey(), entry.getValue());
			message.addMessage(single);
		}
		return message;
	}
}
